import datetime
import tkinter as tk
from tkinter import Label, Toplevel
import threading
import time

class RedstoneReminder:
    def __init__(self, root):
        self.root = root
        self.reminded = {}
        self.load_locations()
        self.today = datetime.date.today()
        self.weekday = self.today.weekday()  # Monday is 0, Sunday is 6

    def load_locations(self):
        self.locations = {
            '1': '暮土',
            '2': '禁阁',
            '3': '云野',
            '4': '雨林',
            '5': '霞谷',
            '6': '暮土',
            '7': '禁阁',
            '8': '云野',
            '9': '雨林',
            '0': '霞谷',
        }
        self.location_details = {
            '暮土': {1: '边陲荒漠', 3: '远古战场', 5: '黑水港湾', 6: '巨兽荒原', 0: '失落方舟'},
            '禁阁': {1: '星光沙漠', 2: '星光沙漠', 4: '星光沙漠搁浅船', 5: '星光沙漠搁浅船', 6: '星光沙漠搁浅船'},
            '云野': {1: '蝴蝶平原', 2: '仙乡', 4: '云顶浮石', 5: '幽光山洞', 6: '圣岛'},
            '雨林': {1: '荧光森林', 2: '密林遗迹', 4: '大树屋', 5: '雨林神殿', 6: '秘密花园'},
            '霞谷': {1: '滑冰场', 2: '滑冰场', 4: '圆梦村', 5: '圆梦村', 6: '雪隐峰'},
        }

    def is_redstone(self, day, weekday):
        return weekday in [5, 6] if day <= 15 else weekday in [4, 6]

    def is_blackstone(self, day, weekday):
        return weekday == 1 if day <= 15 else weekday == 2

    def get_time_ranges(self, weekday, is_redstone_day, is_blackstone_day):
        times = {
            0: [(7, 8), (13, 8), (19, 8)],  # Sunday
            1: [(9, 8), (14, 8), (19, 8)],  # Monday
            2: [(9, 8), (15, 8), (21, 8)],  # Tuesday
            3: [(10, 8), (14, 8), (22, 8)],  # Wednesday
            4: [(11, 8), (17, 8), (23, 8)],  # Thursday
            5: [(10, 8), (14, 8), (22, 8)],  # Friday
            6: [(7, 8), (13, 8), (19, 8)],  # Saturday
        }
        return [time for time in times[weekday] if is_redstone_day or is_blackstone_day]

    def check_time(self, auto_close_seconds):
        # 获取当前时间
        now = datetime.datetime.now()
        current_hour = now.hour
        current_minute = now.minute
        print(f"检查时间: {current_hour:02d}:{current_minute:02d}")
        
        # 获取输出信息
        output = self.output_info()
        print(f"输出信息: {output}")
        
        # 检查是否是红石或黑石出现的时间
        if output != "今天没有红石或黑石出现。":
            for time_range in self.get_time_ranges(self.weekday, self.is_redstone(self.today.day, self.weekday), self.is_blackstone(self.today.day, self.weekday)):
                start_hour, start_minute = time_range
                if current_hour == start_hour and current_minute == start_minute:
                    if not self.reminded.get(time_range, False):
                        self.reminded[time_range] = True
                        self.show_message(output, auto_close_seconds)
                        print(f"弹窗显示: {output}")
                        break
            else:
                print("当前时间未触发任何事件。")
        else:
            print("今天没有红石或黑石出现。")

    def show_message(self, message, auto_close_seconds):
        top = Toplevel(self.root)
        top.title("红黑石提醒")
        top.geometry("288x162")
        frame = tk.Frame(top)
        frame.place(relx=0.5, rely=0.5, anchor="center")
        label = Label(frame, text=message, font=("Arial", 14))
        label.pack()
        top.update_idletasks()
        top_width = top.winfo_width()
        top_height = top.winfo_height()
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()
        x = (screen_width // 2) - (top_width // 2)
        y = (screen_height // 2) - (top_height // 2)
        top.geometry(f"{top_width}x{top_height}+{x}+{y}")
        top.lift()
        if auto_close_seconds > 0:
            top.after(auto_close_seconds * 1000, top.destroy)

    def output_info(self):
        day_suffix = str(self.today.day % 10)
        location = self.locations[day_suffix]
        specific_location = self.location_details.get(location, {}).get(self.weekday, "")
        if self.is_redstone(self.today.day, self.weekday):
            stone_type = "红石"
        elif self.is_blackstone(self.today.day, self.weekday):
            stone_type = "黑石"
        else:
            return "今天没有红石或黑石出现。"
        if specific_location:
            return f"=亲爱的光之子（{stone_type}）来了=\n地点在：{location}：{specific_location}"
        else:
            return f"（{stone_type}）今天没有特定的地点信息。"
